using System.Collections.Generic;
using System.Linq;
using Autofac;
using Autofac.Core;
using BuildBox.Core.BootstrapperTasks;
using BuildBox.Core.Extensions;

namespace BuildBox.Core
{
    public interface IDependencyResolver
    {
        IContainer Container { get; }
    }

    public class DependencyResolver : IDependencyResolver
    {
        private readonly IContainer _container;

        public DependencyResolver(params IModule[] modules)
        {
            ContainerBuilder builder = new ContainerBuilder();

            if (modules != null)
            {
                modules.ForEach(m => builder.RegisterModule(m));
            }

            _container = builder.Build();

            IEnumerable<IBootstrapperTask> tasks = _container.Resolve<IEnumerable<IBootstrapperTask>>();
            tasks.ForEach(x => x.Execute());
        }

        public IContainer Container
        {
            get { return _container; }
        }
    }
}